/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unc.pds.facets;

import unc.pds.auth.IPDSSecurity;
import unc.pds.model.GroupRemote;
import unc.pds.model.UserRemote;

/**
 *
 * @author Mikhail
 */
public class GroupsFacet implements Facet {
    public GroupsFacet(UserRemote user, boolean own, IPDSSecurity security) {
        this.user = user;
        this.own = own;
        this.security = security;
    }
    UserRemote user;
    boolean own;
    IPDSSecurity security;
    public String draw() throws Exception {
        String result = "";
        GroupRemote[] groups = user.getGroups();
        result += "<h4>Группы:</h4>";
        for (int i=0; i<groups.length; i++) {
            result += "<p><a href = \"index.jsp?id="+ groups[i].getKey() +"&view=posts\">"+ groups[i].getName() +"</a>";
            if (own) {
                if (groups[i].getOwner().getKey()!=user.getKey()) {
                    result += " <a href = \"group-processor.jsp?action=quitgroup&id="+ groups[i].getKey() +"\"><img src=\"img/decline.png\" alt=\"Выйти из группы\"/></a>";
                } else {
                    result += " - Владелец";
                }
            }
            if (groups[i].isModer(user.getKey())) {
                result += " - Модератор";
            }
            if (security.getPermission(groups[i].getKey(), "delete")) {
                result += " <a href = \"group-processor.jsp?action=removegroup&id="+ groups[i].getKey() +"\"><img src=\"img/delete.png\" alt=\"Удалить группу\"/></a>";
            }
            result += "</p>\n";
        }
        return result;
    }
    public boolean isSide() {
        return false;
    }
}
